package net.cyl.manager.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.cyl.emums.ShortLinkStateEnum;
import net.cyl.manager.GroupCodeMappingManager;
import net.cyl.mapper.GroupCodeMappingMapper;
import net.cyl.model.GroupCodeMappingDO;
import net.cyl.vo.GroupCodeMappingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class CroupCodeMappingManagerImpl implements GroupCodeMappingManager {
    @Autowired
    private GroupCodeMappingMapper groupCodeMappingMapper;
    @Override
    public GroupCodeMappingDO findByGroupIdAndMappingId(Long mappingId, Long groupId, Long accountNo) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>().eq("mapping_id",mappingId)
                .eq("group_id",groupId)
                .eq("del",0)
                 .eq("account_no",accountNo));
    }

    @Override
    public int add(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.insert(groupCodeMappingDO);
    }

    @Override
    public int del(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.update(null ,new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id",groupCodeMappingDO.getId())
                .eq("account_no",groupCodeMappingDO.getAccountNo())
                .eq("group_id",groupCodeMappingDO.getGroupId())
                .eq("del",0)
                .set("del",1));
    }

    @Override
    public Map<String, Object> pageShortLinkByGroupId(Long groupId, Long accountNo, Integer page, Integer size) {
        Page<GroupCodeMappingDO> pageParam = new Page<>(page, size);

        Page<GroupCodeMappingDO> pageResult = groupCodeMappingMapper.selectPage(pageParam, new QueryWrapper<GroupCodeMappingDO>()
                .eq("group_id",groupId)
                .eq("account_no",accountNo).eq("del",0));

        Map<String, Object> result = new HashMap<>();
        result.put("total_record", pageResult.getTotal());
        result.put("total_page", pageResult.getPages());
        result.put("current_data", pageResult.getRecords().stream()
                .map(this::convert).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int updateGroupCodeMappingState(Long groupId, Long accountNo, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {
        return groupCodeMappingMapper.update(null ,new UpdateWrapper<GroupCodeMappingDO>()
                .eq("code",shortLinkCode)
                .eq("account_no",accountNo)
                .eq("group_id",groupId).eq("del",0).set("state",shortLinkStateEnum.name()));
    }

    @Override
    public GroupCodeMappingDO findByCodeAndGroupId(String shortLinkCode, Long groupId, Long accountNo) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("code",shortLinkCode)
                .eq("group_id",groupId)
                .eq("account_no",accountNo));
    }

    @Override
    public int update(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.update(null,new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id",groupCodeMappingDO.getId())
                .eq("account_no",groupCodeMappingDO.getAccountNo())
                .eq("group_id",groupCodeMappingDO.getGroupId())
                .eq("del",0)
                .set("title",groupCodeMappingDO.getTitle())
                .set("domain",groupCodeMappingDO.getDomain()));

    }


    private GroupCodeMappingVO convert(GroupCodeMappingDO groupCodeMappingDO) {
        GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
        BeanUtils.copyProperties(groupCodeMappingDO,groupCodeMappingVO);
        return groupCodeMappingVO;
    }
}
